package org.robot.power;

import java.io.File;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.robot.Spider;
import org.robot.component.Attachment;

public class Engine {
    private static Log log = LogFactory.getLog(Engine.class);
    
    private boolean running;
    private Spider spider;
    private ThreadPoolExecutor threadPoolExecutor;
    
    public Engine(Spider spider) {
        this.spider = spider;
        int threadNumber = spider.getConfig().getThreadNumber();
        threadPoolExecutor = new ThreadPoolExecutor(threadNumber, threadNumber, 0, 
                TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(threadNumber*3));
    }

    public void start() {
        if (running)
            throw new RuntimeException("爬虫正在运行");
        log.info("启动爬虫，配置为：" + spider.getConfig().toString());
        running = true;
        while (running) {
            if (spider.getSchedule().has() && threadPoolExecutor.getQueue().remainingCapacity() > 0) {
                Attachment attachment = spider.getSchedule().take();
                File destFile = spider.destFile(attachment.getUrl(), attachment.getType(), spider.getConfig().getBaseDirectory());
//                log.debug("submit " + attachment);
                threadPoolExecutor.submit(new Downloader(spider, attachment, destFile));
                if (!destFile.exists())
                    sleep(spider.getConfig().getInterval());
            } else if (threadPoolExecutor.getActiveCount() == 0 && !spider.getSchedule().has())
                break;
        }
        threadPoolExecutor.shutdown();
    }
    
    public void sleep(long millis) {
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
